#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "modbusserver.h"
#include <qdebug.h>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    modbusServer=NULL;


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    //write coil
    modbusServer->setCoilValue(0,true);
    modbusServer->setCoilValue(1,false);
    modbusServer->setCoilValue(2,true);
    modbusServer->setCoilValue(3,false);


}

void MainWindow::on_pushButton_2_clicked()
{
    //read


}

void MainWindow::on_pushButton_3_clicked()
{
    //get coil result

    quint16 valueReturn1,valueReturn2,valueReturn3,valueReturn4;
    modbusServer->getCoilValue(0,valueReturn1);
    modbusServer->getCoilValue(1,valueReturn2);
    modbusServer->getCoilValue(2,valueReturn3);
    modbusServer->getCoilValue(3,valueReturn4);
        qDebug()<<"coil value"<<valueReturn1<<","
                  <<valueReturn2<<","
                    <<valueReturn3<<","
                      <<valueReturn4<<",";



}

void MainWindow::on_pushButton_4_clicked()
{
    if(NULL!=modbusServer)
    {
        delete modbusServer;
    }
    ModbusConnection typeIn=Tcp;
    QString portNameIn="";
    SerialSettings serialSettingIn;
//    QString ip_port_in="127.0.0.1:567";
    QString ip_port_in=ui->lineEdit_ipAndPort->text();
    modbusServer = new ModbusServer(typeIn,portNameIn,serialSettingIn,ip_port_in,1);

    //connect port
    if(1==modbusServer->connectPort())
    {
        qDebug()<<"connect success";
        QMessageBox::information(NULL, "information", "connect success！", QMessageBox::Yes );

    }
    else
    {
        qDebug()<<"connect failed";
        QMessageBox::information(NULL, "information", "connect failed", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_5_clicked()
{
    //disconnect
    if(NULL==modbusServer)
    {
        return ;
    }
    if(1==modbusServer->disConnectPort())
    {
        qDebug()<<"disconnect success";
        QMessageBox::information(NULL, "information", "disconnect success！", QMessageBox::Yes );

    }
    else
    {
        qDebug()<<"disconnect failed";
        QMessageBox::information(NULL, "information", "disconnect failed", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_6_clicked()
{
    if(2==modbusServer->getConnectState())
    {
        QMessageBox::information(NULL, "information", "已经连接", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "information", "未连接", QMessageBox::Yes );
    }

}

void MainWindow::on_pushButton_7_clicked()
{
    //write register
    int registerAdd=ui->lineEdit_registerAddress->text().toInt();
    modbusServer->setHoldingRegisters(registerAdd,ui->lineEdit->text().toInt());

}

void MainWindow::on_pushButton_8_clicked()
{
    //read register
    quint16 returnValue;
    int regAdd=ui->lineEdit_readRegisterAdd->text().toInt();
    modbusServer->getHoldingRegisters(regAdd,returnValue);
    ui->lineEdit_2->setText(QString::number(returnValue));
    qDebug()<<"getHoldingRegisters"<<returnValue;
}

void MainWindow::on_pushButton_9_clicked()
{
    //read result

}
